local table_insert = table.insert
local table_concat = table.concat

log = {}


local time = os.date("%Y-%m-%d %H-%M-%S", os.time())

log.path = ('%s\\log\\%s.log'):format(CURRENT_PATH, time):gsub('\\', '\\\\')

local std_print = print

local function write( str )
    if not log.file then
        require 'filesystem'
        local path = fs.path( ('%s\\log'):format(CURRENT_PATH) )
        if not fs.exists( path ) then
            fs.create_directory( path )
        end
        log.file = io.open(log.path, 'w')

    end
    log.file:write( str )
    log.file:write('\n')
end

local function get_string( ... )
    local ss = {}
    local args = { ... }
    for i = 1, #args do
        table_insert(ss, tostring(args[i]))
    end
    return table_concat(ss, '\t')
end


function log.error(...)
    local s = '[error]:' .. get_string(...)
    local trc = debug.traceback()
	std_print(s)
    std_print(trc)
    write(s)
    write(trc)
end

function log.warn(...)
    local s = '[warn]:' .. get_string(...)
	std_print(s)
    write(s)
end

function log.write(...)
    write(...)
end

function log.info( ... )
    -- local s = '[info]:'.. get_string(...)
    local s = get_string(...)
    std_print(s)
    write(s)
end
